package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.constant.CommonConstant;
import net.wanho.dto.TransshipmentDTO;
import net.wanho.mapper.ProductMapper;
import net.wanho.po.Product;
import net.wanho.po.Transshipment;
import net.wanho.service.TransshipmentService;
import net.wanho.mapper.TransshipmentMapper;
import net.wanho.util.PageInfoUtil;
import net.wanho.vo.PageInfo;
import net.wanho.vo.TransshipmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class TransshipmentServiceImpl extends ServiceImpl<TransshipmentMapper, Transshipment>
    implements TransshipmentService{

    private final ProductMapper productMapper;

    @Override
    public PageInfo<TransshipmentVo> pageList(int pageNum, int pageSize, TransshipmentDTO transshipmentDTO) {
        Page<TransshipmentVo> page = new Page<>(pageNum, pageSize);
        QueryWrapper<TransshipmentVo> wrapper = new QueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(transshipmentDTO.getSourceStoreId()), "s1.store_id", transshipmentDTO.getSourceStoreId());
        wrapper.eq(!ObjectUtils.isEmpty(transshipmentDTO.getTargetStoreId()), "s2.store_id", transshipmentDTO.getTargetStoreId());
        wrapper.like(!ObjectUtils.isEmpty(transshipmentDTO.getSourceProductName()), "p1.product_name", transshipmentDTO.getSourceProductName());
        wrapper.like(!ObjectUtils.isEmpty(transshipmentDTO.getTargetProductName()), "p2.product_name", transshipmentDTO.getTargetProductName());
        wrapper.eq(!ObjectUtils.isEmpty(transshipmentDTO.getCreateName()), "u.user_name", transshipmentDTO.getCreateName());

        Page<TransshipmentVo> p =  this.baseMapper.selectPageList(page, wrapper);
        return PageInfoUtil.convert(page);
    }

    @Override
    public void addTransshipment(Transshipment transshipment) {
        transshipment.setAuditState(CommonConstant.AUDIT_NO);
        this.save(transshipment);
    }

    @Override
    public void confirmTransshipment(Transshipment transshipment) {
        /**
         * 更新源库商品库存
         */
        Product sourceProduct = productMapper.selectById(transshipment.getSourceProductId());
        sourceProduct.setProductInvent(sourceProduct.getProductInvent()-transshipment.getTransNum());
        productMapper.updateById(sourceProduct);

        /**
         * 判断目标仓库是否存在该商品
         */
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getProductNum, sourceProduct.getProductNum());
        wrapper.eq(Product::getStoreId, transshipment.getTargetStoreId());
        Product targetProduct = productMapper.selectOne(wrapper);
        // 如果存在，则更新目标库商品库存
        if (targetProduct != null){
            targetProduct.setProductInvent(targetProduct.getProductInvent()+transshipment.getTransNum());
            productMapper.updateById(targetProduct);

            transshipment.setTargetProductId(targetProduct.getProductId());
        }else{
            // 如果不存在，则新增目标库商品
            Product newProduct = new Product();
            BeanUtils.copyProperties(sourceProduct, newProduct);
            newProduct.setProductInvent(transshipment.getTransNum());
            newProduct.setStoreId(transshipment.getTargetStoreId());
            newProduct.setProductId(null);
            productMapper.insert(newProduct);

            transshipment.setTargetProductId(newProduct.getProductId());
        }

        /**
         * 更新调货单
         */
        transshipment.setAuditState(CommonConstant.AUDIT_YES);
        this.updateById(transshipment);
    }
}




